/**
 *
 * MTG Life Counter
 * Life Counter Application class
 *
 * Created by Nathan Black
 */

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIImageAndTextTableCell.h>
#import <WebCore/WebFontCache.h>
#import "LifeCounterApp.h"

@interface LifeCounterApp (Private)

-(void)applyTheme;

@end

@implementation LifeCounterApp

//////////// Class Methods /////////////////
+ (CGColorRef)colorWithRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha;
{
	float rgba[4] = {red, green, blue, alpha};
	CGColorRef color = (CGColorRef)[(id)CGColorCreate((CGColorSpaceRef)[(id)CGColorSpaceCreateDeviceRGB() autorelease], rgba) autorelease];
	return color;
}

//////////// Table Data /////////////////
- (int) numberOfRowsInTable: (UITable *)table
{
    return 2;
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{
   if (row == 1) return _myLifeCell;
   if (row == 0) return _oppLifeCell;

   return NULL;
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
    reusing: (BOOL) reusing
{
   if (row == 1) return _myLifeCell;
   if (row == 0) return _oppLifeCell;

   return NULL;    
}

- (void)dealloc
{
    [_myLifeCell release];
    [_oppLifeCell release];
    [_table release];
    [_toolbar release];
    [_background release];
    [_window release];

	[super dealloc];
}

////////////// Event Handlers ///////////////
- (void) applicationDidFinishLaunching: (id) unused
{
    NSString* homedir = @"/var/root/Library/LifeCounter";
    if (![[NSFileManager defaultManager] fileExistsAtPath:homedir])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:homedir attributes:nil];
    }

   // init main window here
   _window = [[UIWindow alloc] initWithContentRect: [UIHardware
        fullScreenApplicationContentRect]];
       
   struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
   rect.origin.x = rect.origin.y = 0.0f;

   UIView *mainView = [[UIView alloc] initWithFrame: rect];
    
   [_window orderFront: self];
   [_window makeKey: self];
   [_window _setHidden: NO];

    _table = [[UITable alloc] initWithFrame:rect];
   [_table setScrollingEnabled:NO];
   [_table setRowHeight:rect.size.height / 2.0f];  

   _col1 = [[UITableColumn alloc] initWithTitle: @"col1"
        identifier: @"col1" width: rect.size.width];
   [_table addTableColumn: _col1];
   [_table setBackgroundColor:[LifeCounterApp colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.4f]];    

   _game = [[LifeCounterGame alloc] init];
   _themeMgr = [ThemeManager globalInstance];

   _myLifeCell = [[LifeCounterCell alloc] initWithPlayer:0 game:_game];
   [_myLifeCell setTitle:[NSString stringWithFormat:@"%d", [_game playerLife:0]]];
   [_myLifeCell setShowSelection:NO];
   [_myLifeCell setAlignment:2];
   //[[_myLifeCell titleTextLabel] setEllipsisStyle:0];   
   [[_myLifeCell titleTextLabel] setColor: [LifeCounterApp colorWithRed:1.0f green:0.9f blue:0.9f alpha:1.0f]];   
   _oppLifeCell = [[LifeCounterCell alloc] initWithPlayer:1 game:_game];
   [_oppLifeCell setTitle:[NSString stringWithFormat:@"%d", [_game playerLife:1]]];
   [_oppLifeCell setShowSelection:NO];
   [_oppLifeCell setAlignment:2];
   //[[_myLifeCell titleTextLabel] setEllipsisStyle:1];   
   [[_oppLifeCell titleTextLabel] setColor: [LifeCounterApp colorWithRed:0.9f green:0.9f blue:1.0f alpha:1.0f]];   

    _toolbar = [[LifeCounterToolbar alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, 130.0f, 40.0f)];
    [_toolbar setDelegate:self];
    
    [self applyTheme];

    [_table setDataSource: self];
    [_table setDelegate: self];
    [_table reloadData];
    
    [mainView addSubview: _background];
    [mainView addSubview: _table];
    [mainView addSubview: _toolbar];

    [_window setContentView: mainView]; 
}

- (void)applicationWillTerminate
{
    [_game save];
}

- (void) newGamePressed
{
    [_game newGame];
    [_myLifeCell refresh];
    [_oppLifeCell refresh];
}

- (void) nextThemePressed
{
    NSLog(@"App nextThemePressed");
    [_themeMgr nextTheme];
    [self applyTheme];
}

- (void) prevThemePressed
{
    NSLog(@"App prevThemePressed");
    [_themeMgr prevTheme];
    [self applyTheme];
}

- (void) applyTheme
{
     NSLog(@"applyTheme");
   if (_background == NULL) {
        NSLog(@"new background allocation");
        _background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[_themeMgr currBackgroundFile]]];
    }
    else {
        //UIImage* oldImg = [_background image];
        NSLog(@"before setImage: %@", [_themeMgr currBackgroundFile]);
        [_background setImage:[UIImage imageNamed:[_themeMgr currBackgroundFile]]];
        NSLog(@"before release");
        /*if (oldImg != [_background image]) {
            [oldImg release];
        }
        NSLog(@"after release");*/
    }

    [[_myLifeCell titleTextLabel] setFont:[_themeMgr currFont]];   
    [[_oppLifeCell titleTextLabel] setFont:[_themeMgr currFont]];   
    NSLog(@"after setFont");
}

- (void) historyPressed
{

}

@end
